// Function GMT_key is copied from a script by Mark Wilkinson 
// (http://kremvax.net/firefox/)
// at http://kremvax.net/firefox/googlereadershoworiginal.user.js
// based on code from http://at-aka.blogspot.com/
//
// ==UserScript==
// @name          Google Reader - tall view
// @namespace     http://blog.wtwf.com/
// @description   Hide/Show headers and footers in Google Reader
// @include       http://www.google.com/reader/*
// @include       https://www.google.com/reader/*
// @include       http://reader.google.com/*
// @include       https://reader.google.com/*
// ==/UserScript==

(function() {

var ids = [// header
  'chrome-header', 'viewer-header', 'viewer-footer', 'guser', 'gbar',
  'logo-container','global-info','search','top-nav',
  // footer
  'promo','footer',
  // share
  'drawer',
  'sharing-box-marker','subscribe-area'];

function _greader_toggle_hide_and_show () {
  var length = ids.length;
  var is_visible = document.getElementById(ids[0]).style.display != 'none';
  
  for (var i = 0; i < length; i++){
    var el = document.getElementById(ids[i]);
    if (el) {
      el.style.display = is_visible ? 'none' : 'block';
    }
  }
  var el = document.getElementById('main');
  if (is_visible) {
    el.style.top = '0';
  } else {
    el.style.top = '65px';
  }
  // fire a resize event (this is how we'd like to do it)
  // var ev = document.createEvent('HTMLEvents');
  // ev.initEvent('resize', true, false);
  // document.dispatchEvent(ev);

  // But that doesn;t cause reader to update the screen. 
  // So we do it by toggling something with a class of folder-toggle.
  var el = document.getElementById('your-items-tree-item-0-main');
  // Find the child node with class folder-toggle.
  var kids = el.childNodes;
  for (var i = 0; i < kids.length; i++) {
    if (kids[i].className.match(/(^| )folder-toggle($| )/)) {
      // Send TWO events to toggle it back how it was.
      el = kids[i];
      var ev = document.createEvent("MouseEvents");
      ev.initMouseEvent("click", true, true, window,
			0, 0, 0, 0, 0, 
			false, false, false, false, 0, null);
      el.dispatchEvent(ev);
      el.dispatchEvent(ev);
      break;
    }
  }
}
  
function GRT_key(event) {
  element = event.target;
  elementName = element.nodeName.toLowerCase();
  if (elementName == 'input') {
    typing = (element.type == 'text' || element.type == 'password');
  } else {
    typing = (elementName == 'textarea') || (elementName == 'select');
  }
  if (typing) return true; 
  if (String.fromCharCode(event.which) == 'y' && !event.ctrlKey) {
    _greader_toggle_hide_and_show();
    try {
      event.preventDefault();
    } catch (e) {
    }
    return false;
  }
  return true;
}

document.addEventListener('keypress', GRT_key, false);

})();
